module.exports = function createService(name) {
  let collName = name[0] + name.slice(1) + 's';
  return `
    
  const { mock } = require("mockjs");
  const Common = require("../common/index");
  const dbo = require("../common/database");
  class Service extends Common {
    constructor() {
      super("${collName}");
    } 

    async add(ctx) { 
      try {
        let data = {
          ...ctx.params,
          ${collName.slice(0, -1)}Id: this.uuid(),
        }
        let res = await dbo.insertOne("${collName}", data);
        ctx.body = this.handleSuccess(res);
      } catch (error) {
        ctx.body = this.handleError(error);
      }
    } 

    async list(ctx) {
      let { pageSize=7, currPage=1 } = ctx.params; 
      delete ctx.params.pageSize;
      delete ctx.params.currPage;
      try {
        let list = await dbo.findListByPage("${collName}",pageSize,currPage, {}); 
        let total = await dbo.count('${collName}');
        ctx.body = {
          currPage,
          pageSize,
          total,
          code: 200,
          msg: "success",
          data: list,
        };
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: error.message,
        };
      }
    } 
  
    async all(ctx) {
      try {
        let list = await dbo.findList("${collName}", {});
        let total = await dbo.count('${collName}'); 
        list.forEach(item=> {
          delete item._id;
        })
        ctx.body = {
          total,
          code: 200,
          msg: "success",
          data: list,
        };
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: error.message,
        };
      }
    }

    async find(ctx) {
      try {
        let list = await dbo.findList("${collName}", ctx.params);
        let total = await dbo.count('${collName}');
        ctx.body = {
          total,
          code: 200,
          msg: "success",
          data: list,
        };
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: error.message,
        };
      }
    }
  
    async edit(ctx) {
      let { ${name}Id } = ctx.params;
      delete ctx.params._id;
      ctx.params.updateTime = Date.now();
      try {
        let res = await dbo.updateOne("${collName}", {${name}Id}, ctx.params);
        ctx.body = {
          code: 200,
          msg: "success",
          data: res,
        };
      } catch (error) {
        ctx.body = this.handleError(error);
      }
    }
  
    async detail(ctx) {
      let { ${name}Id } = ctx.params;
      try {
        let res = await dbo.findOne("${collName}", {${name}Id});
        ctx.body = {
          code: 200,
          msg: "success",
          data: res,
        };
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: error.message,
        };
      }
    } 
  
    async del(ctx) {
      try {
        let res = await dbo.deleteOne("${collName}", ctx.params);
        let msg = res.deletedCount>0?'删除成功':'删除失败';
        ctx.body = {
          code: 200,
          msg,
          data: res,
        };
      } catch (error) {
        ctx.body = {
          code: 500,
          msg: error.message,
        };
      }
    }
  }
  module.exports = new Service();  
  `;
};
